PATHMac OS 8 and 9 Developer Documentation > Interapplication Communication > AppleScript for Scripters >

AppleScript Language Guide

   

Examples of Subroutines With Positional Parameters

Here is a subroutine that returns the minimum value of a pair of values.

on minimumValue(x, y)
    if x y then
        return x
    else
        return y
    end if
end minimumValue

-- To call minimum value:
minimumValue(21, 40000)

You can also define a subroutine whose positional parameters define a pattern to match when calling the subroutine. For example, the subroutine that follows takes a single parameter whose pattern consists of two items in a list.

on point({x, y})
    display dialog ("x = " & x & ", y = " & y)
end point

set mypoint to {3, 8}
point(mypoint)

A parameter pattern can be much more complex than a single list. The handler in the next example takes two numbers and a record whose properties include a list of bounds. The handler displays a dialog box summarizing some of the passed information.

on hello(a, b, {length:l, bounds:{x, y, w, h}, name:n})
    set q to a b
    
    set response to "Hello " & n & ", you are " & l & ¬
        " inches tall and occupy position (" & x & ", " & y & ")."
    
    display dialog response
    
end hello

set thing to {bounds:{1, 2, 4, 5}, name:"George", length:72}
hello (2, 3, thing)
--result: A dialog displaying "Hello George, you are 72 inches tall
--          and occupy position (1,2)."

As you can see from this example, a call to a subroutine with patterned parameters can include parameters that aren't literals, as long as they evaluate to the appropriate pattern. Similarly, the properties of a record passed to a subroutine with patterned parameters don't have to be given in the same order in which they are given in the subroutine's definition, as long as all the properties required to fit the pattern are present.


© 1999 Apple Computer, Inc. – (Last Updated 21 May 99)